package com.athena.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.athena.common.utils.StringUtils;
import com.athena.common.core.page.TableDataInfo;
import com.athena.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.athena.crm.domain.bo.AssetDebtInfoBo;
import com.athena.crm.domain.vo.AssetDebtInfoVo;
import com.athena.crm.domain.AssetDebtInfo;
import com.athena.crm.mapper.AssetDebtInfoMapper;
import com.athena.crm.service.IAssetDebtInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 债权信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class AssetDebtInfoServiceImpl implements IAssetDebtInfoService {

    private final AssetDebtInfoMapper baseMapper;

    /**
     * 查询债权信息
     */
    @Override
    public AssetDebtInfoVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询债权信息列表
     */
    @Override
    public TableDataInfo<AssetDebtInfoVo> queryPageList(AssetDebtInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AssetDebtInfo> lqw = buildQueryWrapper(bo);
        List<AssetDebtInfoVo> assetDebtInfoList = baseMapper.selectAssetDebtInfoList(pageQuery.getPageNum(), pageQuery.getPageSize(), lqw);
        Long count = baseMapper.selectAssetDebtInfoCount(lqw);
        log.info("数据长度：{}", assetDebtInfoList.size());
        Page<AssetDebtInfoVo> result = new Page<>();
        result.setRecords(assetDebtInfoList);
        result.setTotal(count);
        result.setPages(pageQuery.getPageSize());
        result.setCurrent(pageQuery.getPageNum());
        return TableDataInfo.build(result);
    }

    /**
     * 查询债权信息列表
     */
    @Override
    public List<AssetDebtInfoVo> queryList(AssetDebtInfoBo bo) {
        LambdaQueryWrapper<AssetDebtInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AssetDebtInfo> buildQueryWrapper(AssetDebtInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AssetDebtInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), AssetDebtInfo::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), AssetDebtInfo::getContent, bo.getContent());
        lqw.eq(bo.getPublishTime() != null, AssetDebtInfo::getPublishTime, bo.getPublishTime());
        lqw.eq(bo.getNumber() != null, AssetDebtInfo::getNumber, bo.getNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getDebtTotal()), AssetDebtInfo::getDebtTotal, bo.getDebtTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getTransferor()), AssetDebtInfo::getTransferor, bo.getTransferor());
        lqw.eq(StringUtils.isNotBlank(bo.getTransferee()), AssetDebtInfo::getTransferee, bo.getTransferee());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), AssetDebtInfo::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getContactNumber()), AssetDebtInfo::getContactNumber, bo.getContactNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), AssetDebtInfo::getCreateBy, bo.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), AssetDebtInfo::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), AssetDebtInfo::getUrl, bo.getUrl());
        return lqw;
    }

    /**
     * 新增债权信息
     */
    @Override
    public Boolean insertByBo(AssetDebtInfoBo bo) {
        AssetDebtInfo add = BeanUtil.toBean(bo, AssetDebtInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改债权信息
     */
    @Override
    public Boolean updateByBo(AssetDebtInfoBo bo) {
        AssetDebtInfo update = BeanUtil.toBean(bo, AssetDebtInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AssetDebtInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除债权信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
